// 初始化场景
import * as THREE from 'three'
import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls'
var camera;
var scene;
var renderer;
export default function(){
   scene = new THREE.Scene();
   const width = window.innerWidth;
   const height = window.innerHeight;
   camera = new THREE.PerspectiveCamera(30,width/height,1,1000);
   camera.position.set(300,100,100);
   camera.lookAt(0,0,0)

   const spotLight = new THREE.SpotLight(0xffffff,5)//,3,300,Math.PI)
   spotLight.position.set(260,80,10);
   spotLight.castShadow = true;
   spotLight.decay = 0
   scene.add(spotLight);

   const geometry = new THREE.BoxGeometry(50,50,50);
   const material = new THREE.MeshStandardMaterial({
    color: 0xff00ff
   })
   
   const mesh = new THREE.Mesh(geometry,material);
   mesh.position.set(0,0,0)
   scene.add(mesh) 

   renderer = new THREE.WebGLRenderer();
   renderer.setSize(width,height);
   renderer.setClearColor(0xcccccc);
 
   document.body.appendChild(renderer.domElement)
   var trackballControls = new TrackballControls(camera,renderer.domElement);

   function renderScene(){
      requestAnimationFrame(renderScene);
      trackballControls.update();
      renderer.render(scene,camera)
   }
   renderScene()   
}

window.addEventListener("resize",onResize,false)
function onResize(){
   camera.aspect = window.innerWidth/window.innerHeight;
   camera.updateProjectionMatrix();
   renderer.setSize(window.innerWidth,window.innerHeight);
 }